<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="服务商" prop="supplier_id">
        <el-select v-model="form.supplier_id" placeholder="请选择服务商">
          <el-option
            v-for="supplier in suppliers"
            :key="supplier.id"
            :label="supplier.supplier_name"
            :value="supplier.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input v-model="form.price" />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input v-model.number="form.number" />
      </el-form-item>
      <el-form-item label="备注" prop="mark">
        <el-input v-model="form.mark" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/dc_projectExpenses'
  import { pullTempuserlist as getSupplierList } from '@/api/dc_projectExpenses'
  import { isNum, isNumber } from '@/utils/validate'

  export default {
    name: 'ActivityExpensesEdit',

    data() {
      return {
        form: {
          activity_id: null,
          supplier_id: null,
          project_category: '',
          detail: '',
          price: '',
          number: '',
          mark: '',
        },
        rules: {
          supplier_id: [
            { required: true, trigger: 'change', message: '请选择供应商' },
          ],
          project_category: [
            { required: true, trigger: 'blur', message: '请输入项目类别' },
          ],
          detail: [{ required: true, trigger: 'blur', message: '请输入细项' }],
          price: [
            { required: true, trigger: 'blur', message: '请输入单价' },
            {
              validator(rule, value, callback) {
                if (isNum(value)) callback()
                else callback('请输入最多两位小数的数字')
              },
              trigger: 'blur',
            },
          ],
          number: [
            { required: true, trigger: 'blur', message: '请输入数量' },
            {
              validator(rule, value, callback) {
                if (value && isNumber(value)) callback()
                else callback('请输入正整数')
              },
              trigger: 'blur',
            },
          ],
        },
        suppliers: [],
        title: '',
        dialogFormVisible: false,
        type: null,
      }
    },

    methods: {
      showEdit(row) {
        this.form.activity_id = +this.$route.query.activity_id // 标记费用所属活动
        if (!row) {
          this.title = '添加'
          this.type = 'add'
        } else {
          this.title = '编辑'
          this.type = 'edit'
          this.form = { ...this.form, ...row, activity_expenses_id: row.id }
        }
        this.initSuppliers()
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let msg
            console.log(this.form)
            if (this.type === 'add') msg = await doAdd(this.form)
            else msg = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      initSuppliers() {
        let params = {
          activity_id: this.$route.query.activity_id
        }
        getSupplierList(params).then((res) => {
          this.suppliers = res
        })
      },
    },
  }
</script>
